package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.Chat;
import com.jokerpan.project.service.ChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author jokerpan
 */
@RestController
@RequestMapping("chat")
@Api("私信获取Api")
public class ChatController {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    ChatService chatService;


    @GetMapping("/queryChat")
    @PreAuthorize("hasPermission('','user:list')")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有私信记录")
    public ListResult<Object> queryList(Chat chat){
        return ResultBuilder.buildListSuccess(chatService.queryChat(chat));
    }

    @PostMapping("/add")
    @ApiOperation("新增私信")
    @AccessLimit(seconds = 6, maxCount = 5)
    @PreAuthorize("hasPermission('','user:update')")
    public ActionResult addAction(@RequestBody Chat chat){
        if (chatService.add(chat)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed("操作失败！");
        }
    }


    @GetMapping("/deleteById")
    @PreAuthorize("hasPermission('','user:update')")
    @ApiOperation("根据id删除私信记录")
    @AccessLimit(seconds = 6, maxCount = 5)
    public ActionResult deleteById(@RequestParam("id") Integer id){
        if (chatService.deleteById(id)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

}
